package main

import (
	"fmt"
	"net"
)


func process(conn net.Conn)  {

	defer conn.Close()

	for  {
		bytes := make([]byte, 1024)
		fmt.Printf("accept ip =%v: \t", conn.RemoteAddr().String())
		read, err := conn.Read(bytes)
		if err != nil {
			fmt.Println("read err=", err)
			return
		}
		//控制台打印显示内容
		fmt.Print(string(bytes[:read]))
	}
}

/*
	tcp协议发送数据
 */
func main() {
	fmt.Println("开始监听端口...")
	//监听8888端口
	listen, err := net.Listen("tcp", "0.0.0.0:8888")
	if err != nil {
		fmt.Println("listen err=", err)
		return
	}

	defer listen.Close()

	for {
		//监听端口，并会阻塞
		accept, err := listen.Accept()
		if err != nil {
			println("监听失败 err", err)
		}else {
			//开启线程
			go process(accept)
		}

	}
}
